/*
 * @lc app=leetcode.cn id=372 lang=cpp
 *
 * [372] 超级次方
 */

// @lc code=start
class Solution
{
  const int MOD = 1337;

  int pow(int x, int n)
  {
    int res = 1;
    while (n) {
      if (n % 2) {
        res = (long) res * x % MOD;
      }
      x = (long) x * x % MOD;
      n /= 2;
    }
    return res;
  }

public:
  int superPow(int a, vector<int>& b)
  {
    int ans = 1;
    for (int i = b.size() - 1; i >= 0; --i) {
      ans = (long) ans * pow(a, b[i]) % MOD;
      a = pow(a, 10);
    }
    return ans;
  }
};
// @lc code=end
